/* 公共request 方法 */
const ajax = (
	method="POST",
	url,
	params,
) => {
	// 请求头存储商户id
	let token = uni.getStorageSync('token') || '';
	let shareuid = uni.getStorageSync('shareuid') || '';
	let header = {
		'content-type': 'application/json; charset=UTF-8',
		'token': token,
		'shareuid':shareuid
	}
	uni.setStorageSync('header',header)
	// 判断是否有网络
	let isNetWork = true
	uni.getNetworkType({
		success: function(res) {
			if (res.networkType == 'none') {
				isNetWork = false
			}
		}
	})

	if (isNetWork) {
		return new Promise(function(resolve, reject) {
				uni.request({
					url: 'http://test.hzajy.cn/api/' + url,
					method: method,
					data: params,
					header: header,
					sslVerify: false,
					success: (res) => {
						// console.log(res, 'http')
						resolve(res.data)
					},
					fail: function(res) {
						uni.showToast({
							title: '网络延迟，请稍后重试',
							icon: 'none',
							duration: 2000,
							mask: true
						})
					}
				})
			})
			.catch((res) => {})
	} else {
		uni.showToast({
			title: '请开启网络',
			icon: 'none',
			duration: 2000,
			mask: true
		})
	}
}

export default ajax
